package com.duck.duckchatbackend;

import com.duck.duckchatbackend.redis.RedisUtils;
import com.duck.duckchatbackend.websocket.netty.NettyWebSocketStarter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;


@Component("initRun")
public class InitRun implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(InitRun.class);
    @Resource
    private DataSource dataSource;
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private NettyWebSocketStarter nettyWebSocketStarter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
       try{
           dataSource.getConnection();
           redisUtils.get("test");
           new  Thread(nettyWebSocketStarter).start();
           logger.info("数据库连接正常，Redis连接正常");
       }catch (SQLException e){
           logger.error("数据库连接异常");
       }catch (RedisConnectionFailureException e){
           logger.error("Redis连接异常");
       }catch (Exception e) {
           logger.error("服务器启动失败",e);
       }
    }
}
